package cn.zzszxyy.hr.controller;

import cn.zzszxyy.hr.comm.Global;
import cn.zzszxyy.hr.comm.StringUtil;
import cn.zzszxyy.hr.model.UserEntity;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 作者:张伟
 * <p>
 * 联系:adamzzww@163.com
 * 时间:2016-11-27
 * 说明:无
 */
public class ControllerEx {

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    public Integer getPageIndex(HttpServletRequest request) {
        String pageIndex = request.getParameter("pageIndex") == null ? "1" : request.getParameter("pageIndex");
        return StringUtil.isEmpty(pageIndex) ? 1 : Integer.parseInt(pageIndex);
    }

    public UserEntity getCurUser(HttpServletRequest request) {
        UserEntity user = (UserEntity) request.getSession().getAttribute(Global.JSP_USER_OBJECT);
        return user;
    }

}
